package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageSaleMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageSaleOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.BatchSpecListBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageNewBatchBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageSaleBatchListBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageSaleOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageSaleNewBatch;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageSaleSelectBatchRequest;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class RefundStorageSaleItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStorageSaleOverviewBean.SaleBodyBean.ItemListBean> mRefundStorageItemList;
    private LayoutInflater inflater;
    private RefundStorageSaleOverviewActivity.RefundStorageSaleOverviewListener mRefundStorageSaleOverviewListener;
    private String mMaterielCode, mSaleNumber, mContractNumber, mCustomerName, mSaleOrderType, mSaleOrganization, mSaleGroup, mRemark;

    public RefundStorageSaleItemListAdapter(Context context, RefundStorageSaleOverviewBean.SaleBodyBean data, List<RefundStorageSaleOverviewBean.SaleBodyBean.ItemListBean> refundStorageItemList, String materielCode, RefundStorageSaleOverviewActivity.RefundStorageSaleOverviewListener refundStorageSaleOverviewListener) {
        mContext = context;
        mSaleNumber = data.getRefer_receipt_code();
        mContractNumber = data.getPreorder_id();
        mCustomerName = data.getCustomer_name();
        mSaleOrderType = data.getOrder_type_name();
        mSaleOrganization = data.getSale_org_name();
        mSaleGroup = data.getSale_group_name();
        mRemark = data.getRemark();
        mRefundStorageItemList = refundStorageItemList;
        mRefundStorageSaleOverviewListener = refundStorageSaleOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
        LogUtils.i("inAdapter ======= " + SPUtils.getSaleRefundStorageInformation(context));
    }

    @Override
    public int getCount() {
        return mRefundStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mRefundStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_refund_storage_sale_item_list_first_item, null);
            TextView saleNumber = (TextView) convertView.findViewById(R.id.saleNumber);
            TextView contractNumber = (TextView) convertView.findViewById(R.id.contractNumber);
            TextView customerName = (TextView) convertView.findViewById(R.id.customerName);
            TextView saleOrderType = (TextView) convertView.findViewById(R.id.saleOrderType);
            TextView saleOrganization = (TextView) convertView.findViewById(R.id.saleOrganization);
            TextView saleGroup = (TextView) convertView.findViewById(R.id.saleGroup);
            TextView remark = (TextView) convertView.findViewById(R.id.remark);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            saleNumber.setText(mSaleNumber);
            contractNumber.setText(mContractNumber);
            customerName.setText(mCustomerName);
            saleOrderType.setText(mSaleOrderType);
            saleOrganization.setText(mSaleOrganization);
            saleGroup.setText(mSaleGroup);
            remark.setText(mRemark);
            materielCode.setText(mMaterielCode);
            materielCode.setHint(mContext.getResources().getString(R.string.activity_in_storage_receive_overview_hint));
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mRefundStorageSaleOverviewListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mRefundStorageSaleOverviewListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            RefundStorageSaleOverviewBean.SaleBodyBean.ItemListBean refundStorageItem = mRefundStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_refund_storage_sale_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, refundStorageItem, position);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(R.id.serialNumber);
        viewHolder.realSerialNumber = (TextView) view.findViewById(R.id.realSerialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.batchNumber = (TextView) view.findViewById(R.id.batchNumber);
        viewHolder.unit = (TextView) view.findViewById(R.id.unit);
        viewHolder.refundStorageNumberMax = (TextView) view.findViewById(R.id.refundStorageNumberMax);
        viewHolder.batchInfoJson = (TextView) view.findViewById(R.id.batchInfoJson);
        viewHolder.refundStorageNumber = (EditText) view.findViewById(R.id.refundStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
        viewHolder.retainDecimal = (TextView) view.findViewById(R.id.retainDecimal);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStorageSaleOverviewBean.SaleBodyBean.ItemListBean refundStorageItem, int position) {
        viewHolder.serialNumber.setText(position + "");
        viewHolder.realSerialNumber.setText(refundStorageItem.getRid());
        viewHolder.materielCode.setText(refundStorageItem.getMat_code());
        viewHolder.materielDescribe.setText(refundStorageItem.getMat_name());
        viewHolder.retainDecimal.setText(refundStorageItem.getDecimal_place());
//        viewHolder.batchNumber.setText(TextUtils.isEmpty(refundStorageItem.getBatchNumber()) ? context.getResources().getString(R.string.activity_refund_storage_sale_materiel_detail_choose_batch_number) : refundStorageItem.getBatchNumber());
        if (TextUtils.isEmpty(refundStorageItem.getBatch())) {
            if (refundStorageItem.getBatchList() == null || refundStorageItem.getBatchList().isEmpty()) {
                viewHolder.batchNumber.setText(context.getResources().getString(R.string.activity_refund_storage_sale_materiel_detail_choose_batch_number));
            } else {
                viewHolder.batchNumber.setText(mContext.getResources().getString(R.string.activity_refund_storage_reserced_materiel_detail_use_the_new_batch));
            }
        } else {
            viewHolder.batchNumber.setText(refundStorageItem.getBatch());
        }
        if (!TextUtils.isEmpty(refundStorageItem.getBatch())) {
            viewHolder.batchInfoJson.setText(new Gson().toJson(refundStorageItem.getBatchInfoList()));
        }
        viewHolder.unit.setText(refundStorageItem.getUnit_name());
        String refundStorageNumberMaxStr = String.valueOf(Float.parseFloat(refundStorageItem.getOrder_qty()) - Float.parseFloat(refundStorageItem.getHave_return_qty()));
        viewHolder.refundStorageNumberMax.setText(refundStorageNumberMaxStr);
        viewHolder.refundStorageNumber.setText(TextUtils.isEmpty(refundStorageItem.getRefundNumber()) ? "" : refundStorageItem.getRefundNumber());
        if (RefundStorageUtils.checkSaleItem(refundStorageItem)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }
        viewHolder.batchNumber.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                RefundStorageSaleSelectBatchRequest.request(context, refundStorageItem.getMat_code(), refundStorageItem.getFty_code(), new GsonBeanListener<RefundStorageSaleBatchListBean>() {
                    @Override
                    public void onSuccess(RefundStorageSaleBatchListBean bean) {
                        List<RefundStorageSaleBatchListBean.SaleBatchBodyBean> batchList = bean.getBody();
                        if (batchList == null || batchList.isEmpty()) {
                            Utils.middleToast(context, mContext.getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                            batchList = new ArrayList<>();
                        }
//                        else {
                        DialogUtils.getRefundStorageSaleBatchDialog(context, batchList, new DialogUtils.RefundStorageSaleBatchNumberListener() {
                            @Override
                            public void onSelect(Dialog dialog, String batchNumberStr, List<RefundStorageSaleBatchListBean.SaleBatchBodyBean.BatchSpecListBean> batchInfoList) {
                                viewHolder.batchNumber.setText(batchNumberStr);
                                viewHolder.batchInfoJson.setText(new Gson().toJson(batchInfoList));
                            }

                            //新建批次
                            @Override
                            public void onNewBatch() {
//                                RefundStorageSaleNewBatch.request(context, refundStorageItem.getFty_code(), refundStorageItem.getMat_code(), mSaleNumber, new GsonBeanListener<RefundStorageNewBatchBean>() {
//                                    @Override
//                                    public void onSuccess(RefundStorageNewBatchBean bean) {
//                                        DialogUtils.getNewBatchDialog(context, bean, refundStorageItem.getRid(), new DialogUtils.RefundStorageSaleNewBatchNumberListener() {
//                                            @Override
//                                            public void onNewBatch(List<RefundStorageNewBatchBean.DataBean.BatchBean> list) {
//                                                viewHolder.batchNumber.setText(mContext.getResources().getString(R.string.activity_refund_storage_reserced_materiel_detail_use_the_new_batch));
//                                                RefundStorageUtils.saveNewBatchList(context, list, refundStorageItem.getRid());
//                                            }
//                                        }).show();
//                                    }
//                                });

                                List<BatchSpecListBean> list = new ArrayList<>();
                                list.addAll(refundStorageItem.getBatch_material_spec_list());
                                list.addAll(refundStorageItem.getBatch_spec_list());
                                DialogUtils.getNewBatchDialog(context, list, refundStorageItem.getRid(), new DialogUtils.RefundStorageSaleNewBatchNumberListener() {
                                    @Override
                                    public void onNewBatch(List<BatchSpecListBean> list) {
                                        viewHolder.batchNumber.setText(mContext.getResources().getString(R.string.activity_refund_storage_reserced_materiel_detail_use_the_new_batch));
                                        RefundStorageUtils.saveNewBatchList(context, list, refundStorageItem.getRid());
                                    }
                                }).show();
                            }
                        }).show();
//                        }
                    }
                });
            }
        });
        viewHolder.materielDescribe.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mRefundStorageSaleOverviewListener.onCheckData()) {
                    Intent intent = new Intent(context, RefundStorageSaleMaterielDetailActivity.class);
                    intent.putExtra(RefundStorageExtra.SERIAL, viewHolder.serialNumber.getText().toString());
                    intent.putExtra(RefundStorageExtra.REAL_SERIAL, viewHolder.realSerialNumber.getText().toString());
                    intent.putExtra(RefundStorageExtra.SALE_NUMBER, mSaleNumber);
                    context.startActivity(intent);
                }
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, batchNumber, unit, refundStorageNumberMax, batchInfoJson, realSerialNumber, retainDecimal;

        private EditText refundStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
